package lumis.service.comment;

import org.w3c.dom.Node;

import lumis.content.ContentManagerFactory;
import lumis.content.core.ContentVersion;
import lumis.content.service.DetailsUtil;
import lumis.doui.table.filter.TableSourceFilter;
import lumis.portal.PortalException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.util.query.IQueryValue;
import lumis.util.query.QueryValue;

/**
 * A filter that gets its value from the itemId from the details interface if it 
 * is not found normally, and uses the corresponding contentLocaleId as its value.
 * 
 * @version $Revision: 8800 $ $Date: 2008-01-22 11:38:36 -0200 (Tue, 22 Jan 2008) $
 * @since 4.0.10
 */
public class CommentContentFilter extends TableSourceFilter
{
	@Override
	protected IQueryValue calculateFilterValue(Node filterNode) throws PortalException
	{
		String itemId = null;
		IQueryValue value = super.calculateFilterValue(filterNode);
		if (value != null && value instanceof QueryValue)
			itemId = (String)((QueryValue)value).getValue();
		
		if(itemId == null)
			itemId = DetailsUtil.getCurrentItemId();
		
		if (itemId != null)
		{
			try
			{
				ContentVersion contentVersion = ContentManagerFactory.getContentManager().getContentVersionByItemId(itemId);
				return new QueryValue(contentVersion.getContentLocale().getId());
			}
			catch (PortalObjectNotFoundException e)
			{
				return null;
			}
		}
		else
		{
			return null;
		}
	}
}
